<%@ include file="Common.jsp" %><%!
//
//   Filename: EditMatches.jsp
//   Generated with CodeCharge  v.1.2.0
//   JSP.ccp build 05/21/01
//

static final String sFileName = "MatchBets.jsp";
              
%><%

String cSec = checkSecurity(1, session, response, request);
if ("sendRedirect".equals(cSec) ) return;
                
boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sMemberId = (String) session.getAttribute("UserID");
String sMatchesErr = "";

String sMatchType = getParam( request, "type");
String sFromPage = getParam( request, "FromPage");

java.sql.Connection conn = null;
java.sql.Statement stat = null;

String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("Matches") ) {
  sMatchesErr = MatchesAction(request, response, session, out, sAction, sForm, conn, stat, sMatchType, sFromPage);
  if ( "sendRedirect".equals(sMatchesErr)) return;
}

%>            
<html>
<head>
<title>Zoccer, das Fussball Tippspiel - Begegnung</title>

</head>
<body style="background-color: #ffffff; color: #000000; font-family: Arial, Tahoma, Verdana, Helveticabackground-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helvetica">
<jsp:include page="Header.jsp" flush="true"/>

<% Matches_Show(request, response, session, out, sMatchesErr, sForm, sAction, conn, stat, sMatchType, sFromPage); %>

<jsp:include page="Footer.jsp" flush="true"/>
</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!

  
  
  String MatchesAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat, String type, String fromPage) throws java.io.IOException {
  
    String sMatchesErr ="";
    try {

      if (sAction.equals("")) return "";

      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "Matches.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

      String pPKbet_id = "";
      
      if (sAction.equalsIgnoreCase("cancel") ) {
        try {
          if ( stat != null ) stat.close();
          if ( conn != null ) conn.close();
        }
        catch ( java.sql.SQLException ignore ) {}        
        
        response.sendRedirect (sActionFileName + "?type=" + type + "&FormMatches_Page=" + fromPage);
        return "sendRedirect";
      }

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }
      //if ( sAction.equalsIgnoreCase("delete") ) { iAction = ideleteAction; }

      // Create WHERE statement

      if ( iAction == iupdateAction || iAction == ideleteAction ) { 
        pPKbet_id = getParam(request, "bet_id");
        if (isEmpty(pPKbet_id)) return sMatchesErr;       
        sWhere = "bet_id=" + toSQL(pPKbet_id, adNumber);
      }

      String fldmember_id="";
      String fldmatch_id="";
      String fldbet_id="";
      String fldhome_guess="";
      String fldguest_guess="";
      String fldguest="";
      String fldhome="";
      String fldbet_tip="";
      String fldscore="";
      String fldtimeout="";
      String fldgroup="";
      String fldven="";

      // Load all form fields into variables
    
      fldmember_id = getParam(request, "member_id");
      fldmatch_id = getParam(request, "match_id");
      fldbet_id = getParam(request, "bet_id");
      fldhome_guess = getParam(request, "home_guess");
      fldguest_guess = getParam(request, "guest_guess");
      fldguest = getParam(request, "guest");
      fldhome = getParam(request, "home");
      fldven = getParam(request, "ven");
	  fldtimeout = getParam(request, "timeout");

	  // Validate params
	  if (isEmpty(fldmatch_id)) {
		  return sMatchesErr;
	  }

	  // Validate timeout
	  java.util.Date dbTimeout = getDate(fldmatch_id);
	  if (dbTimeout != null && isTimedOut(dbTimeout)) {
		  sMatchesErr += "No Update. Match is timed out.";
		  return sMatchesErr;
	  }  

	  // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if ( isEmpty(fldhome_guess) ) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp H\".<br>";
        }        
        if ( ! isNumber(fldhome_guess)) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp H\".<br>";
        }
        if ( isNegative(fldhome_guess)) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp H\".<br>";
        }
        
        if ( isEmpty(fldguest_guess) ) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp G\".<br>";
        }
        if ( ! isNumber(fldguest_guess)) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp G\".<br>";
        }        
        if ( isNegative(fldguest_guess)) {
          sMatchesErr = sMatchesErr + "Invalid value in field \"Tipp G\".<br>";
        }
        
        if (sMatchesErr.length() > 0 ) {
          return (sMatchesErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
        case iinsertAction :
            sSQL = "insert into bets (match_id, member_id, home_guess, guest_guess, timestamp)" +
                   " values (" + 
                	toSQL(fldmatch_id, adNumber) + "," +
                        toSQL(fldmember_id, adNumber) + "," +
                        toSQL(fldhome_guess, adNumber) + "," +
                        toSQL(fldguest_guess, adNumber) + "," +
						"current_timestamp)";
            break;
  
      	case iupdateAction:
            sSQL = "update bets set " +
                "home_guess=" + toSQL(fldhome_guess, adNumber) + ", " +
                "guest_guess=" + toSQL(fldguest_guess, adNumber)+ "," +
				"timestamp = current_timestamp";
            sSQL = sSQL + " where " + sWhere;
            break;
      
      	case ideleteAction:
           sSQL = "delete from Matches where " + sWhere;
           break;
      }

      if ( sMatchesErr.length() > 0 ) return sMatchesErr;
      try {
        // Execute SQL statement
        stat.executeUpdate(sSQL);
      }
      catch(java.sql.SQLException e) {
        sMatchesErr = e.toString(); return (sMatchesErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName + "?type=" + type + "&FormMatches_Page=" + fromPage);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sMatchesErr);
  }


  void Matches_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sMatchesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat, String type, String fromPage) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      java.sql.Statement tmpStat = null;
      
      String pbet_id = "";

      String fldmember_id="";
      String fldmatch_id="";
      String fldbet_id="";
      String fldhome_guess="";
      String fldguest_guess="";
      String fldguest="";
      String fldhome="";
      String fldguest_id="";
      String fldhome_id="";
      String fldbet_tip="";
      String fldscore="";
      String fldtimeout="";
      String fldgroup="";
      String fldven="";

      boolean bPK = true;

      if ( "".equals(sMatchesErr)) {
        // Load primary key and form parameters        
        fldbet_id = getParam( request, "bet_id");
        pbet_id = getParam( request, "bet_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmember_id = getParam( request, "member_id");
        fldmatch_id = getParam( request, "match_id");

		// SM2004-06-08 BUG-Bets1 fixed (id -> bet_id)
		fldbet_id = getParam( request, "bet_id");
        fldhome_guess = getParam(request, "home_guess");
        fldguest_guess = getParam(request, "guest_guess");
        fldscore = getParam(request, "score");
        fldhome = getParam(request, "home");
        fldguest = getParam(request, "guest");
        fldtimeout = getParam(request, "timeout");
        fldgroup = getParam(request, "tgroup");
        fldven = getParam(request, "ven");
        
        pbet_id = getParam( request, "bet_id");
      }
      
      if ( isEmpty(pbet_id)) { bPK = false; }      
      
      sWhere += "bet_id=" + toSQL(pbet_id, adNumber);      
      primaryKeyParams += "<input type=\"hidden\" name=\"PK_bet_id\" value=\""+pbet_id+"\"/>";
      sSQL = "select * from bets where " + sWhere;

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        
        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
              
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        fldbet_id = (String) rsHash.get("bet_id");
        
        if ( "".equals(sMatchesErr)) {                
        // Load data from recordset when form displayed first time
          fldmember_id = (String) rsHash.get("member_id");
          fldmatch_id = (String) rsHash.get("match_id");
          fldbet_id = (String) rsHash.get("bet_id");          
          fldhome_guess = (String) rsHash.get("home_guess");
          fldguest_guess = (String) rsHash.get("guest_guess");
          fldscore = (String) rsHash.get("score");
          
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
          fldven = (String) dLookUp (tmpStat, "matches", "ven", "match_id=" + fldmatch_id);
        }

        if (sAction.equals("") || ! "Matches".equals(sForm)) {
          fldmember_id = (String) rsHash.get("member_id");
          fldmatch_id = (String) rsHash.get("match_id");
          fldbet_id = (String) rsHash.get("bet_id");         
          fldhome_guess = (String) rsHash.get("home_guess");
          fldguest_guess = (String) rsHash.get("guest_guess");
          fldscore = (String) rsHash.get("score");
          
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
          fldven = (String) dLookUp (tmpStat, "matches", "ven", "match_id=" + fldmatch_id);
        }
      }
      else {
        
        if ( "".equals(sMatchesErr)) {
          fldbet_id = toHTML(getParam(request,"bet_id"));
          fldmatch_id = toHTML(getParam(request,"match_id"));
          
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
          fldven = (String) dLookUp (tmpStat, "matches", "ven", "match_id=" + fldmatch_id);
        }
      }      
      
      out.println("<center>");      
      out.println("&nbsp;<br><br><br><br>");    
      out.println("<table width=\"70%\" bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"1\">");
      out.println("<tr>\n"+
      		"<td style=\"background:rgb(235,235,235)\" colspan=\"7\"><center><font style=\"font-size: 14pt; font-weight: bold\">Tipp-Abgabe f&uuml;r Spiel Nummer " + toHTML((String) getParam(request, "match_id")) + "</font></center></td>\n</tr>");
      if ( ! sMatchesErr.equals("")) {
        out.println("<tr>\n<td style=\"background:rgb(235,235,235)\" colspan=\"7\"><font style=\"font-size: 10pt; color: #ffffff\">"+sMatchesErr+"</font></td>\n</tr>");
      }
      sMatchesErr="";
      out.println("<form method=\"get\" action=\"" + sFileName + "?type=" + getParam(request, "type") + "&FormMatches_Page=" + getParam(request, "FromPage") + "\" name=\"Matches\">");

      // Show form field
      out.print("<tr>\n");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Anstoss</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Gruppe</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Heim</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Tipp H</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Tipp G</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Gast</font></td>");
      out.print("<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; font-weight: bold\">Austragungsort</font></td>");
      out.println("\n</tr>");
            
      out.print("<tr>\n");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">&nbsp;" + toHTML(fldtimeout) + "</font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">&nbsp;" + toHTML(fldgroup) + "</font></td>");
      out.print("<td width=\"120\" style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">&nbsp;" + toHTML(fldhome) + " <img src=\"../images/" + fldhome + ".gif\" alt=\"???\"/></font></td>");      
      out.print("<td align=\"right\" style=\"background:rgb(235,235,235)\"><input tabindex=\"1\" type=\"text\" name=\"home_guess\" maxlength=\"2\" value=\"" + toHTML(fldhome_guess) + "\" size=\"2\">");      
      out.print("<td style=\"background:rgb(235,235,235)\"><input tabindex=\"2\" type=\"text\" name=\"guest_guess\" maxlength=\"2\" value=\"" + toHTML(fldguest_guess) + "\" size=\"2\">");            
      out.print("<td width=\"120\" style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">&nbsp;" + "<img src=\"../images/" + fldguest + ".gif\" alt=\"???\"/> " + fldguest + "</font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(fldven) + "</font></td>");
      out.println("\n</tr>");
      
      out.print("<tr>\n<td bgcolor=\"ffffff\" colspan=\"7\" align=\"right\">");

      if ( bPK && ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        out.print("<input type=\"submit\" value=\"Update\" onclick=\"document.Matches.FormAction.value = 'update';\"> ");        
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        //out.print("<input type=\"submit\" value=\"Delete\" onclick=\"document.Matches.FormAction.value = 'delete';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\">");
        out.print("<input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Insert\" onclick=\"document.Matches.FormAction.value = 'insert';\">");
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\"><input type=\"hidden\" value=\"insert\" name=\"FormAction\">");
      }
      
      out.print("<input type=\"hidden\" name=\"bet_id\" value=\""+toHTML(fldbet_id)+"\">");
      out.print("<input type=\"hidden\" name=\"home\" value=\""+toHTML(fldhome)+"\">");
      out.print("<input type=\"hidden\" name=\"guest\" value=\""+toHTML(fldguest)+"\">");
      out.print("<input type=\"hidden\" name=\"timeout\" value=\""+toHTML(fldtimeout)+"\">");
      out.print("<input type=\"hidden\" name=\"ven\" value=\""+toHTML(fldven)+"\">");
      out.print("<input type=\"hidden\" name=\"group\" value=\""+toHTML(fldgroup)+"\">");
      out.print("<input type=\"hidden\" name=\"match_id\" value=\""+toHTML(fldmatch_id)+"\">");
      out.print("<input type=\"hidden\" name=\"type\" value=\""+ type +"\">");
      out.print("<input type=\"hidden\" name=\"FromPage\" value=\""+ fromPage +"\">");
      out.print("<input type=\"hidden\" name=\"member_id\" value=\""+toHTML((String) session.getAttribute("UserID"))+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n</tr>\n</form>\n</table>");
      out.println("&nbsp;<br><br>");      
      out.println(getRandomCit());
      out.println("<br>");
      out.println("</center>");

    }
    catch (Exception e) { out.println(e.toString()); }
  } %>